'use strict'
export function setPages(datas, currentPage = 1, isdone = false, search = '') {
    let total = 0, pageSize = 5;
    if (datas.length !== 0) {

        datas = datas.filter(x => x.isDone == isdone);
        datas = datas.filter(
            data =>
                !search ||
                data.content.toLowerCase().includes(search.toLowerCase())
        );
        datas.sort((a, b) => {
            if (b.isDone !== a.isDone) {
                return b.isDone - a.isDone;
            } else if (b.level !== a.level) {
                return b.level - a.level;
            } else {
                return new Date(b.date) - new Date(a.date);
            }
        });
        total = datas.length;
        if (currentPage > Math.ceil(total / pageSize)) {
            currentPage--;
        }
        var d = []
        for (var i = (currentPage - 1) * pageSize;i < currentPage * pageSize;i++) {
            if (datas[i]) {
                d.push(datas[i]);
            }
        }
        return {
            datas: d,
            currentPage,
            pageSize,
            total
        }
    } else {
        return {
            datas,
            currentPage,
            pageSize,
            total
        }
    }
}
export function hasDone(data, isdone = true) {
    if (localStorage.getItem('todo') !== '') {
        let datas = JSON.parse(localStorage.getItem('todo'));
        for (let i = 0;i < datas.length;i++) {
            if (datas[i].id === data.id) {
                datas[i].isDone = isdone;
            }
        }
        localStorage.setItem('todo', JSON.stringify(datas));
    }

}